package com.stone.db.jdbc.service.impl;

import com.stone.db.jdbc.entity.Order;
import com.stone.db.jdbc.mapper.OrderMapper;
import com.stone.db.jdbc.service.OrderService;
import com.sun.tools.corba.se.idl.constExpr.Or;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>Created with IntelliJ IDEA. </p>
 * <p>User: Stony </p>
 * <p>Date: 2016/6/27 </p>
 * <p>Time: 14:49 </p>
 * <p>Version: 1.0 </p>
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {

    @javax.annotation.Resource
    OrderMapper orderMapper;

    @Override
    public List<Order> queryAll() {
        return orderMapper.selectAll();
    }

    @Override
    public Order saveOrder(Order order) {
        orderMapper.insert(order);
        return order;
    }

    @Override
    public Order updateOrder(Order order) {
        orderMapper.update(order);
        return order;
    }

    @Override
    public Order queryOrder(int orderId) {
        List<Order> orders = orderMapper.queryOrderById(orderId);
        if(orders != null && orders.size() >= 1){
            return orders.get(0);
        }
        return null;
    }

    @Override
    public int saveOrders(List<Order> orders) {
        int x = 0;
        for(Order order : orders){
            x++;
            saveOrder(order);
        }
        return x;
    }

    @Override
    public void saveOrdersWithFailure(List<Order> orders) {
        saveOrders(orders);
        throw new IllegalArgumentException("failed");
    }
}
